package com.zttek.videoShareProject.common.base;

import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.videoShareProject.common.json.Result;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 13296 on 2017/8/12.
 */
public class MyBaseController {
    /**
     * ajax失败
     * @return {Object}
     */
    public Object renderError() {
        Result result = new Result();
        return result;
    }

    /**
     * ajax成功
     *
     * @return {Object}
     */
    public Object renderSuccess() {
        Result result = new Result();
        result.setSuccess(true);
        return result;
    }

    /**
     * ajax成功
     * @param obj 成功时的对象
     * @return {Object}
     */
    public Object renderSuccess(Object obj) {
        Result result = new Result();
        result.setSuccess(true);
        result.setObj(obj);
        return result;
    }

    public <T> Page<T> getPage(int current, int size, String sort, String order){
        Page<T> page = new Page<T>(current, size, sort);
        if ("desc".equals(order)) {
            page.setAsc(false);
        } else {
            page.setAsc(true);
        }
        return page;
    }

    /**
     * 获取项目basePath
     * @param request
     * @return
     */
    public String getBasePath(HttpServletRequest request){
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path;
        return basePath;
    }
}
